package com.ibuonline.b2b.common.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;

import com.ibuonline.b2b.common.SysCache;
import com.ibuonline.b2b.entity.BaseOption;

public class BaseOptionSelectTag extends BaseCustomerTag {

	private static final long serialVersionUID = 4938037565086504235L;
    
	private String name;
	private String type;
	private boolean group;
	private String grouplabel;
	private String headName;
	private String headValue;
	private String size;
	
	public int doStartTag() throws JspException {
		StringBuilder sb = new StringBuilder();
		String paramValue = pageContext.getRequest().getAttribute(name)+"";
		sb.append("<select name='").append(name).append("'");
		generateAttribute(sb);
		if(size != null) {
			sb.append(" size='").append(size).append("'");
		}
		sb.append(">");

		if (headName != null) {
			sb.append("<option value='").append(headValue).append("' >")
			  .append(headName).append("</option>");
		}
		
		String[] types = type.split(",");
		String[] grouplabels = null;
		int len = types.length;
		if (len > 1) {
			if (grouplabel != null) {
				grouplabels = grouplabel.split(",");
			}
		}

		for (int i=0; i<len; i++) {
			if (group) {
				sb.append("<optgroup label='").append(grouplabels[i]).append("'>");
			}
			List<BaseOption> baseOptions = SysCache.getOptionByType(types[i]);
			for(BaseOption option : baseOptions) {
				sb.append("<option value='").append(option.getOptionValue()).append("'");
				if (!"null".equals(paramValue)) {
					if (paramValue.equals(option.getOptionValue())) {
						sb.append(" selected='selected' ");
					}
				} else if (option.getOptionValue().equals(defaultValue)) {
					// when action didn't pass values to the JSP and current option value 
					// equals default value, let current option checked. Added by LinYufa (2011-12-30)
					sb.append(" selected='selected' ");
				}
				sb.append(">").append(option.getOptionName()).append("</option>\n");
			}
			if (group) {
				sb.append("</optgroup>");
			}
		}
		sb.append("</select>\n");
		try {
			this.pageContext.getOut().print(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public boolean isGroup() {
		return group;
	}

	public void setGroup(boolean group) {
		this.group = group;
	}

	public String getGrouplabel() {
		return grouplabel;
	}

	public void setGrouplabel(String grouplabel) {
		this.grouplabel = grouplabel;
	}

	public String getHeadName() {
		return headName;
	}

	public void setHeadName(String headName) {
		this.headName = headName;
	}

	public String getHeadValue() {
		return headValue;
	}

	public void setHeadValue(String headValue) {
		this.headValue = headValue;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}
}
